import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluwx/fluwx.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/model/my/local_file_model.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 下载分享页面
class DownloadSharePage extends StatefulWidget {
  /// 相关数据
  final LocalFileModel model;

  const DownloadSharePage({
    super.key,
    required this.model,
  });

  @override
  State<DownloadSharePage> createState() => _DownloadSharePageState();
}

class _DownloadSharePageState extends State<DownloadSharePage> {
  late BuildContext _context;

  @override
  Widget build(BuildContext context) {
    debugPrint("downloadSharePage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    _context = context;
    return Container(
      padding: EdgeInsets.all(30),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(30),
          topRight: Radius.circular(30),
        ),
      ),
      child: Column(
        children: [
          _titleItem(),
          hiSpace(height: 30),
          Expanded(child: _content(context)),
        ],
      ),
    );
  }

  /// 标题组件显示
  _titleItem() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          "分享",
          style: FontUtil.fontBlack1(
            32,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  /// 内容显示
  _content(BuildContext context) {
    // 计算宽度
    double width = MediaQuery.of(context).size.width;
    double tabWidth = (width - 60) / 4;
    return ListView(
      children: [
        Row(
          children: [
            _tabItem(
              iconUrl: "images/my/download_share_weixin.svg",
              text: "微信好友",
              tabWidth: tabWidth,
              onTapFun: _onTapFunWeiXin,
            )
          ],
        )
      ],
    );
  }

  /// 图标内容显示
  _tabItem({
    required String iconUrl,
    required String text,
    required double tabWidth,
    required Function() onTapFun,
  }) {
    return GestureDetector(
      onTap: onTapFun,
      child: Container(
        width: tabWidth,
        color: colorWhite1,
        child: Column(
          children: [
            SvgPicture.asset(
              iconUrl,
              width: 44,
              height: 44,
            ),
            hiSpace(height: 20),
            Text(
              text,
              style: FontUtil.fontBlack1(22),
            )
          ],
        ),
      ),
    );
  }

  /// 微信分享点击方法
  _onTapFunWeiXin() {
    debugPrint("downloadSharePage 页面 -> 微信分享点击 ===> ");
    _futureShareWeiXin().then((Map<String, dynamic> map) {
      if (!map["suc"]) {
        ToastUtil.show(context: _context, msg: map["msg"]);
      }
    });
  }

  /// 微信具体分享方法
  Future<Map<String, dynamic>> _futureShareWeiXin() async {
    Map<String, dynamic> map = {};
    try {
      Fluwx fluwx = Fluwx();
      File file = File(widget.model.path);
      bool value = await fluwx.share(WeChatShareFileModel(
        WeChatFile.file(
          file,
          suffix: ".${widget.model.type}",
        ),
        title: widget.model.name,
        scene: WeChatScene.session,
      ));

      if (value) {
        map["suc"] = true;
      } else {
        map["suc"] = false;
        map["msg"] = "分享失败";
      }
    } catch (e) {
      debugPrint("downloadSharePage 页面 -> 微信分享发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        debugPrint("downloadSharePage 页面 -> dao异常 ===> ${e.msg}");
        map["msg"] = e.msg;
      }
    }
    return map;
  }
}
